import router from './router'
import store from './store'
const whiteList = ['/login', '/404'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // determine whether the user has logged in
  console.log(store.getters.userToken)
  if (store.getters.userToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next('/')
    } else {
      if (!store.state.user.userInfo.userId) {
        const { roles } = await store.dispatch('user/getInfo')
        store.dispatch('permission/filterRoutes', roles)
        next(to.path)
      } else {
        next()
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

